package com.yxzr.youyou;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.adapter.RechargeAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.RechargeBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RechargeActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private RechargeAdapter adapter;
    private RelativeLayout recharge_rel_alipay,recharge_rel_wechat;
    private CheckBox recharge_checkbox_alipay,recharge_checkbox_wechat;
    private TextView recharge_tv_money,recharge_tv_desc;
    private List<RechargeBean.DataBean> list = new ArrayList<>();
    private int payType = 1;
    private double price = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        StatusBarUtil.setStatusBarColor(this, R.color.recharge_title);
        setTitleWithBack();
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recharge_rv);
        recharge_rel_alipay = findViewById(R.id.recharge_rel_alipay);
        recharge_rel_wechat = findViewById(R.id.recharge_rel_wechat);
        recharge_checkbox_alipay = findViewById(R.id.recharge_checkbox_alipay);
        recharge_checkbox_wechat = findViewById(R.id.recharge_checkbox_wechat);
        recharge_tv_money = findViewById(R.id.recharge_tv_money);
        recharge_tv_desc = findViewById(R.id.recharge_tv_desc);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(20,3));
        adapter = new RechargeAdapter(this);
        mRecyclerView.setAdapter(adapter);

        adapter.setOnClickListener(new RechargeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                adapter.setSelectIndex(position);

                price = Double.parseDouble(list.get(position).getOld_price()) - Double.parseDouble(list.get(position).getPrice());
                recharge_tv_money.setText(list.get(position).getPrice());
                recharge_tv_desc.setText("已优惠"+price+"元");
            }
        });


        //支付宝
        recharge_rel_alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 1;
                recharge_checkbox_alipay.setChecked(true);
                recharge_checkbox_wechat.setChecked(false);
            }
        });

        recharge_checkbox_alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 1;
                recharge_checkbox_wechat.setChecked(false);
            }
        });

        //微信
        recharge_rel_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 2;
                recharge_checkbox_alipay.setChecked(false);
                recharge_checkbox_wechat.setChecked(true);
            }
        });

        recharge_checkbox_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 2;
                recharge_checkbox_alipay.setChecked(false);
            }
        });
        initData();
    }
    private void initData(){
        RequestUtil.getInstence().API().recharge()
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        RechargeBean bean = gson.fromJson(t,RechargeBean.class);

                        list = bean.getData();
                        adapter.clearList();
                        adapter.addList(bean.getData());

                        price = Double.parseDouble(list.get(0).getOld_price()) - Double.parseDouble(list.get(0).getPrice());
                        recharge_tv_money.setText(list.get(0).getPrice());
                        recharge_tv_desc.setText("已优惠"+price+"元");
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
